<?php

/**
 * AbetInstructor form base class.
 *
 * @method AbetInstructor getObject() Returns the current form's model object
 *
 * @package    sfproject
 * @subpackage form
 * @author     Your name here
 */
abstract class BaseAbetInstructorForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'ins_code'             => new sfWidgetFormInputHidden(),
      'ins_name'             => new sfWidgetFormInputText(),
      'ins_NicName'          => new sfWidgetFormInputText(),
      'ins_sex'              => new sfWidgetFormInputText(),
      'ins_rank_code'        => new sfWidgetFormInputText(),
      'ins_current_load'     => new sfWidgetFormInputText(),
      'dep_code'             => new sfWidgetFormPropelChoice(array('model' => 'AbetDepartment', 'add_empty' => true)),
      'ins_special_role'     => new sfWidgetFormInputCheckbox(),
      'abet_department_list' => new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'AbetCollege')),
    ));

    $this->setValidators(array(
      'ins_code'             => new sfValidatorChoice(array('choices' => array($this->getObject()->getInsCode()), 'empty_value' => $this->getObject()->getInsCode(), 'required' => false)),
      'ins_name'             => new sfValidatorString(array('max_length' => 255)),
      'ins_NicName'          => new sfValidatorString(array('max_length' => 255)),
      'ins_sex'              => new sfValidatorString(array('max_length' => 1)),
      'ins_rank_code'        => new sfValidatorString(array('max_length' => 10)),
      'ins_current_load'     => new sfValidatorInteger(array('min' => -2147483648, 'max' => 2147483647)),
      'dep_code'             => new sfValidatorPropelChoice(array('model' => 'AbetDepartment', 'column' => 'dep_code', 'required' => false)),
      'ins_special_role'     => new sfValidatorBoolean(),
      'abet_department_list' => new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'AbetCollege', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('abet_instructor[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'AbetInstructor';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['abet_department_list']))
    {
      $values = array();
      foreach ($this->object->getAbetDepartments() as $obj)
      {
        $values[] = $obj->getColCode();
      }

      $this->setDefault('abet_department_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveAbetDepartmentList($con);
  }

  public function saveAbetDepartmentList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['abet_department_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(AbetDepartmentPeer::INS_CODE, $this->object->getPrimaryKey());
    AbetDepartmentPeer::doDelete($c, $con);

    $values = $this->getValue('abet_department_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new AbetDepartment();
        $obj->setInsCode($this->object->getPrimaryKey());
        $obj->setColCode($value);
        $obj->save();
      }
    }
  }

}
